﻿#region 文件信息

// FileName: SoCafeShop/Sohobloo.SoAuth.Web/RoleController.cs
// Author: Sohobloo
// CreateTime: 2013/07/01 5:03
// UpdateTime: 2013/07/01 5:57

#endregion

#region 引用

using System;
using System.ComponentModel.Composition;
using System.Linq;
using System.Web.Mvc;
using Sohobloo.SoAuth.Entity;
using Sohobloo.SoAuth.Service;
using Sohobloo.SoAuth.Web.Models;

#endregion

namespace Sohobloo.SoAuth.Web.Controllers
{
    [Export]
    public class RoleController : Controller
    {
        [Import]
        public IRoleService RoleService { get; set; }

        //
        // GET: /Role/

        public ActionResult Index()
        {
            var roleList = RoleService.GetRoleList();

            var roleModeList = roleList.Select(
                r => new RoleModel
                {
                    Id = r.Id,
                    RoleName = r.RoleName,
                    RoleDescription = r.RoleDescription,
                    RoleEnabled = r.RoleEnabled
                });

            return View(roleModeList);
        }

        public ActionResult Add()
        {
            var roleModel = new RoleModel();

            return View(roleModel);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Add(RoleModel roleModel)
        {
            var role = new SoAuthRole
            {
                RoleName = roleModel.RoleName.Trim(),
                RoleDescription = roleModel.RoleDescription != null ? roleModel.RoleDescription.Trim() : null,
                RoleEnabled = roleModel.RoleEnabled
            };

            RoleService.CreateRole(role);

            return RedirectToAction("Index");
        }

        public ActionResult Edit(Guid id)
        {
            var role = RoleService.GetRole(id);
            var roleMode = new RoleModel
            {
                Id = role.Id,
                RoleName = role.RoleName,
                RoleDescription = role.RoleDescription,
                RoleEnabled = role.RoleEnabled
            };

            return View(roleMode);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Edit(RoleModel roleModel)
        {
            var role = RoleService.GetRole(roleModel.Id);
            role.RoleName = roleModel.RoleName;
            role.RoleDescription = roleModel.RoleDescription != null ? roleModel.RoleDescription.Trim() : null;
            role.RoleEnabled = roleModel.RoleEnabled;

            RoleService.UpdateRole(role);

            return RedirectToAction("Index");
        }

        public ActionResult Delete(Guid id)
        {
            RoleService.DeleteRole(id);

            return RedirectToAction("Index");
        }
    }
}